<style lang="less">
@import "./messageManage.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card>
                    <Row>
                        <Form ref="searchForm" :model="searchForm" inline :label-width="80" class="search-form">
                            <Form-item label="发送用户ID" prop="name">
                              <Input type="text" v-model="userId" placeholder="请输入发送用户完整ID" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item style="margin-left:-35px;" class="br">
                              <Button @click="getDataList" type="primary" icon="ios-search">搜索</Button>
                              <Button @click="getDataList(0)" >重置</Button>
                            </Form-item>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button v-hasRole="'ROLE_ADMIN'" @click="delAll" icon="md-trash">批量删除</Button>
                        <Button @click="getDataList" icon="md-refresh">刷新</Button>
                    </Row>
                     <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort" @on-selection-change="changeSelect"></Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page :current="pageNumber" :total="total" :page-size="pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
    </div>
</template>

<script>
import { getMessageSendData, deleteMessageSend } from "@/api/index";
export default {
  name: "message-send-detail",
  data() {
    return {
      loading: true, // 表单加载状态
      messageId: "",
      sortColumn: "ms.createTime", // 排序字段
      sortType: "desc", // 排序方式
      selectList: [], // 多选数据
      selectCount: 0, // 多选计数
      columns: [
        // 表头
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "发送消息标题",
          key: "messageTitle"
        },
        {
          title: "发送用户",
          key: "userName",
          sortable: true
        },
        {
          title: "状态",
          key: "messageStatus",
          align: "center",
          sortable: true,
          render: (h, params) => {
            if (params.row.messageStatus === 0) {
              return h("div", [
                h(
                  "Tag",
                  {
                    props: {
                      color: "default"
                    }
                  },
                  "未读"
                )
              ]);
            } else if (params.row.status === 1) {
              return h("div", [
                h(
                  "Tag",
                  {
                    props: {
                      color: "green"
                    }
                  },
                  "已读"
                )
              ]);
            } else if (params.row.status === 2) {
              return h("div", [
                h(
                  "Tag",
                  {
                    props: {
                      color: "orange"
                    }
                  },
                  "回收站"
                )
              ]);
            }
          }
        },
        {
          title: "创建时间",
          key: "createTime",
          sortable: true,
          sortType: "desc"
        },
        {
          title: "更新时间",
          key: "updateTime",
          sortable: true
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          render: (h, params) => {
            if (this.getStore("roles").includes("all")) {
              return h("div", [
                h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small",
                      icon: "md-trash"
                    },
                    on: {
                      click: () => {
                        this.remove(params.row);
                      }
                    }
                  },
                  "删除"
                )
              ]);
            }
          }
        }
      ],
      data: [], // 表单数据
      pageNumber: 1, // 当前页数
      pageSize: 10, // 页面大小
      total: 0, // 表单数据总数
      userId: ""
    };
  },
  methods: {
    init() {
      this.messageId = this.$route.query.id;
      this.getDataList();
    },
    changePage(v) {
      this.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.pageSize = v;
      this.getDataList();
    },
    changeSort(e) {
      this.sortColumn = e.key;
      this.sortType = e.order;
      if (e.order === "normal") {
        this.sortType = "";
      }
      this.getDataList();
    },
    getDataList(v) {
      this.loading = true;
      if (v === 0) {
        this.userId = "";
      }
      let params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize,
        sort: this.sortColumn,
        order: this.sortType,
        messageId: this.messageId,
        status: "",
        userId: this.userId
      };
      getMessageSendData(params).then(res => {
        this.loading = false;
        if (res.status === true) {
          this.data = res.data.rows;
          this.total = res.data.total;
        }
      });
    },
    remove(v) {
      this.$Modal.confirm({
        title: "确认删除",
        // 记得确认修改此处
        content: "您确认要删除该条数据?",
        onOk: () => {
          // 删除
          deleteMessageSend(v.id).then(res => {
            if (res.status === true) {
              this.$Message.success(res.info);
              this.getDataList();
            }
          });
        }
      });
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    changeSelect(e) {
      this.selectList = e;
      this.selectCount = e.length;
    },
    delAll() {
      if (this.selectCount <= 0) {
        this.$Message.warning("您还未选择要删除的数据");
        return;
      }
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
        onOk: () => {
          let ids = "";
          this.selectList.forEach(function(e) {
            ids += e.id + ",";
          });
          ids = ids.substring(0, ids.length - 1);
          // 批量删除
          deleteMessageSend(ids).then(res => {
            if (res.status === true) {
              this.$Message.success("操作成功");
              this.clearSelectAll();
              this.getDataList();
            }
          });
        }
      });
    }
  },
  watch: {
    // 监听路由变化通过id获取数据
    $route(to, from) {
      if (to.name === "message_send_detail") {
        this.messageId = this.$route.query.id;
        this.getData();
      }
    }
  },
  mounted() {
    this.init();
    this.$Modal.info({
      title: "提示",
      content: "该页面已根据用户角色隐藏操作按钮"
    });
  }
};
</script>